@polkadot/util-crypto
Various useful cyrpto utility functions that are used across all projects in the @polkadot namespace. It provides utility functions with additional safety checks, allowing not only for consistent coding, but also reducing the general boilerplate.
Usage
Installation -
yarn add @polkadot/util-crypto
Functions can be imported as follows:
import { mnemonicGenerate } from '@polkadot/util-crypto';
Documentation and Available Utilities
Below is a list of currently exposed methods published at the Polkadot-JS Common Documentation Portal.
- util-crypto
- Functions
- Blake2b
- Keccak
- Mnemonic
- Nacl
- Random
- SHA-512
- xxHash
Blake2b
- Creates a blake2b u8a output containing a hash from a u8a input bytes using given bit-length or default bit-length of 256. An optional key u8a of up to 64 bytes may be provided
- Creates a blake2b hex string with optionally specified bit-length from the u8a input or default bit-length used is 256
- Uses the Blake2b hash function of blakejs library
- Note: All hashes used in Polkadot-JS are 32 bytes (i.e. 256 / 8)
- Note: BLAKE hash functions are in the Nacl library
- Note: Only handles up to 2**53 bytes of input
Keccak
- Creates a Keccak-256 hex string from the input (such as for conversion from seed phrase)
- Creates a Keccak-256 Uint8Array from the input
- Uses the Keccak-256 hash function from the js-sha3 library
Mnemonic
- Generate valid mnemonics (for account seed phrase) using BIP39
- Converts mnemonics to a valid seed secret with Uint8Array output
Nacl
- Implements NaCl secret-key authenticated encryption, public-key authenticated encryption, hashing, and public-key signatures
- Decrypts a message that has been encrypted with a secret key using Nacl
- Encrypts a message with a secret key and generates an associated nonce using Nacl
- Signing a message with a secret key to generate a valid signature using Nacl
- Verifies a signed message by providing the message, signature, and public key (associated with the secret key that signed it) using Nacl
Random
- Returns a sequence of secure random bytes in a variety of formats
- Creates a u8a with a specified (optional) bit-length (default 32) filled with random bytes
- Creates a hex string with a specified (optional) bit-length (default 32) filled with random bytes
- Creates a random number from random secure bytes
SHA-512
- Implements SHA-512 hashing functions for a variety of input and outputs
- Creates a SHA-512 hash of u8a input
- Uses the SHA-512 hash function of tweetnacl library
xxHash
- Create xxhash64 values with specified bitlengths
- Creates a xxhash64 u8a from the input with a specified (optional) bit-length (default 64)
- Creates a xxhash64 hex string from the input with a specified (optional) bit-length (default 64)
- Uses xxHash to generate values as BN, hex & number output